<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索框区域 -->
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="2">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="100px"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="90px"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <!-- 编辑按钮区域 -->
            <!-- {{ scope.row }} -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoodsById(scope.row.goods_id)"
            ></el-button>
            <!-- 删除按钮区域 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="goodsSizeChange"
        @current-change="goodsCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsSaved">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'List',
  data() {
    return {
      // 定义获取商品信息的请求对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      // 获取所有商品信息
      goodsList: [],
      // 定义总商品条数
      total: 0,
      // 隐藏添加商品对话框
      editDialogVisible: false,
      // 修改商品的表单
      editForm: {
        goods_id: 0,
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
        // 选中的父级分类的 id 数组
        pics: [],
        goods_introduce: '',
        attrs: [],
      },
      //修改商品表单数据的规则验证
      editFormRules: {
        goods_name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur',
          },
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur',
          },
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 监听获取商品信息列表事件
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })

      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }

      this.goodsList = res.data.goods
      this.total = res.data.total

      // console.log(this.goodsList, this.total)
    },
    // 监听分页选择的页数改变事件
    goodsSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听分页输入的页数内容改变事件
    goodsCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 监听添加商品事件
    addGoods() {
      this.$router.push('/goods/add')
    },
    // 监听删除商品事件
    async deleteGoods(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete('goods/' + id)

      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      // console.log(res.data)
      this.$message.success('删除成功')
      this.$bus.$emit('SaveList', res.data)
      this.getGoodsList()
    },
    // 监听编辑事件获取商品信息事件
    async editGoodsById(id) {
      const { data: res } = await this.$http.get('goods/' + id)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.editForm = res.data
      // 显示编辑商品对话框
      this.editDialogVisible = true
    },
    async editGoodsSaved() {
      const { data: res } = await this.$http.put(
        'goods/' + this.editForm.goods_id,
        {
          goods_name: this.editForm.goods_name,
          goods_price: this.editForm.goods_price,
          goods_weight: this.editForm.goods_weight,
          goods_number: this.editForm.goods_number,
        }
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
    },
  },
}
</script>

<style lang="less" scoped>
</style>